﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.ClientEngine;
using System.Net;
using System.Threading.Tasks;

namespace TestClient
{
    public class DeviceClient
    {
        public EasyClient<ResponsePackageInfo> client = null;

        public DeviceClient()
        {

        }

        public void ConnectToServer()
        {
            client = new EasyClient<ResponsePackageInfo>();
            client.Initialize(new ResponseReceiveFilter());
            client.Connected += OnClientConnected;
            client.NewPackageReceived += OnPackageReceived;
            client.Error += OnClientError;
            client.Closed += OnClientClosed;
            Task<bool> t = client.ConnectAsync(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3333));
            t.Wait();

        }

        private void OnPackageReceived(object sender, PackageEventArgs<ResponsePackageInfo> e)
        {
            //LogHelper.WriteLog("收到文本下行：" + e.Package.Body);
            //16进制
            //LogHelper.WriteLog("下行：" + DataHelper.ByteToHex(e.Package.Header, e.Package.Header.Length) + DataHelper.ByteToHex(e.Package.Data, e.Package.Data.Length));
            //回复服务端确认         
        }

        private void OnClientConnected(object sender, EventArgs e)
        {
            //LogHelper.WriteLog("已连接到服务器...");
        }

        private void OnClientClosed(object sender, EventArgs e)
        {
          
            //LogHelper.WriteLog("连接已断开...");
        }

        private void OnClientError(object sender, ErrorEventArgs e)
        {
            //LogHelper.WriteLog("客户端错误：" + e.Exception.Message);
        }
    }
}
